package com.joker.discovery;

import com.joker.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author 调用其他微服务的帮助类
 */
@Component
public class InformationDiscovery {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    public User instances(String userId){
        List<ServiceInstance> informationList = discoveryClient.getInstances("information");
        ServiceInstance serviceInstance = informationList.get(0);
        String url = serviceInstance.getUri() + "/user/findByUserId/"+ userId;
        User user = this.restTemplate.getForObject(url,User.class);
        return user;
    }

}
